﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ninject.Syntax;
using Ninject.Planning.Bindings;

namespace CoolCode.Ioc
{
    class Binder<T> : IBind<T>
    {
        IBindingToSyntax<T> _Inner;
        internal IBinding Binding;

        public Binder()
        {
            Type service = typeof(T);
            Binding = new Binding(service);
            _Inner = new BindingBuilder<T>(Binding);
        }

        public Binder(IBindingToSyntax<T> syntax)
        {
            _Inner = syntax;
        }

        #region IBind<T> Members

        public IHold<T> To<TImp>() where TImp : T
        {
            return Wrap(_Inner.To<TImp>());
        }

        public IHold<T> To(Type type)
        {
            return Wrap(_Inner.To(type));
        }

        #endregion

        private IHold<T> Wrap(IBindingWhenInNamedWithOrOnSyntax<T> syntax)
        {
            return new Holder<T>(syntax);
        }
    }

    class Holder<T> : IHold<T>
    {
        private IBindingWhenInNamedWithOrOnSyntax<T> _Inner;

        public Holder(IBindingWhenInNamedWithOrOnSyntax<T> syntax)
        {
            _Inner = syntax;
        }

        #region IHold<T> Members

        public void Singleton()
        {
            _Inner.InSingletonScope();
        }

        public void Request()
        {
            _Inner.InRequestScope();
        }

        #endregion
    }

}
